const ACTION_PREFIX = module.id; // 设置action前缀确保type的唯一性
// console.log('ACTION_PREFIX1', ACTION_PREFIX);

export const GET_USER_INFO_REQUEST = `${ACTION_PREFIX}/GET_USER_INFO_REQUEST`;
export const getUserInfoRequest = () => ({
  type: GET_USER_INFO_REQUEST,
});

export const GET_USER_INFO_SUCCESS = `${ACTION_PREFIX}/GET_USER_INFO_SUCCESS`;
export const getUserInfoSuccess = userInfo => ({
  type: GET_USER_INFO_SUCCESS,
  userInfo,
});

export const GET_USER_INFO_FAIL = `${ACTION_PREFIX}/GET_USER_INFO_FAIL`;
export const getUserInfoFail = () => ({
  type: GET_USER_INFO_FAIL,
});

export function getUserInfo() {
  return async dispatch => {
    dispatch(getUserInfoRequest());

    try {
      const response = await fetch('/src/mock/userInfo.json');
      const json = await response.json();
      dispatch(getUserInfoSuccess(json));
    } catch (e) {
      // dispatch(getUserInfoFail());
      dispatch(getUserInfoSuccess({ name: 'tikeyc' }));
    }
  };
}

// ////////////// redux-axios-middleware 的action示例代码 //////////////
export const LOAD_INIT_DATA = `${ACTION_PREFIX}/LOAD_INIT_DATA`;
export const LOAD_INIT_DATA_SUCCESS = `${ACTION_PREFIX}/LOAD_INIT_DATA_SUCCESS`;
export const LOAD_INIT_DATA_FAIL = `${ACTION_PREFIX}/LOAD_INIT_DATA_FAIL`;
export const loadInitData = params => ({
  type: LOAD_INIT_DATA,
  payload: {
    request: {
      method: 'POST',
      // url: 'data://userInfo.json',
      url: '/src/mock/userInfo.json',
      params,
    },
  },
});
